//
//  SHRegistView.m
//  SimpleHappy
//
//  Created by 张超 on 15/11/24.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "SHRegistView.h"
#import "Define.h"
#import <BmobSDK/Bmob.h>

@interface SHRegistView ()

@property (nonatomic, strong) UITextField *cPasswd;
@property (nonatomic, strong) UITextField *email;


@end

@implementation SHRegistView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 重写父类
    [super myTitleLabel].text = @"注册";
    [[super registBtn]removeFromSuperview];

    [super userId].placeholder = @"输入帐号";
    [super pwd].placeholder = @"输入密码";
    [super pwd].secureTextEntry = YES;

    
    // 再次输入密码
    self.cPasswd = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 1.2, KScreenWidth / 12.5)];
    self.cPasswd.secureTextEntry = YES;
    self.cPasswd.center = CGPointMake(KScreenWidth / 2, [super pwd].frame.origin.y + KScreenWidth / 4.17);
    self.cPasswd.placeholder = @"再次输入密码";
    [self.view addSubview:self.cPasswd];
    
    // 邮箱
    self.email = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 1.2, KScreenWidth / 12.5)];
    self.email.center = CGPointMake(KScreenWidth / 2, self.cPasswd.frame.origin.y + KScreenWidth / 4.17);
    self.email.placeholder = @"输入邮箱 (用于密码重置)";
    [self.view addSubview:self.email];

    
    // 密码下划线
    UIView *pwdView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [super pwd].bounds.size.width, 1)];
    pwdView.backgroundColor = [UIColor blackColor];
    pwdView.center = CGPointMake(KScreenWidth / 2, self.pwd.frame.origin.y + self.pwd.bounds.size.height);
    [self.view addSubview:pwdView];
    
    // 确认密码下划线
    UIView *cPwdView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [super pwd].bounds.size.width, 1)];
    cPwdView.backgroundColor = [UIColor blackColor];
    cPwdView.center = CGPointMake(KScreenWidth / 2, self.cPasswd.frame.origin.y + [super pwd].bounds.size.height);
    [self.view addSubview:cPwdView];
    
    // 邮箱下划线
    UIView *emailView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [super pwd].bounds.size.width, 1)];
    emailView.backgroundColor = [UIColor blackColor];
    emailView.center = CGPointMake(KScreenWidth / 2, self.email.frame.origin.y + [super pwd].bounds.size.height);
    [self.view addSubview:emailView];
    
    // 验证并注册
    [[super loginBtn]removeFromSuperview];
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame = CGRectMake((KScreenWidth - KScreenWidth / 3) / 2, KScreenHeight / 1.6, KScreenWidth / 3, KScreenWidth / 7);
    [self.loginBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(registBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];

}




#pragma mark - 注册按钮
- (void)registBtnAction:(UIButton *)btn
{
    BmobUser *bUser = [[BmobUser alloc]init];
    // 上传账号
    if ([super userId].text.length >= 6 || [super pwd].text.length >= 6) {
        [bUser setUsername:[super userId].text];
    }else{
        // 警告框
        UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"账号或密码不能小于6位" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    
    // 上传密码
    // 判断两次密码输入是否一样
    if ([[super pwd].text isEqualToString:self.cPasswd.text]) {
        [bUser setPassword:[super pwd].text];
    }else{
        // 警告框
        UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"密码输入不一致" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    
    // 上传用户邮箱
    if ([self isValidateEmail:self.email.text]) {
        [bUser setEmail:self.email.text];
    }else{
        // 警告框
        UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"邮箱格式错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
        return;

    }
    
    // 判断是否注册成功
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            NSLog(@"注册成功");
            // 警告框
            UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"注册成功！" message:@"已发送验证邮件到您的注册邮箱!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];

            
        }else{
            NSLog(@"%@",error.userInfo);
            // 警告框
            UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"注册失败！" message:[NSString stringWithFormat:@"原因:%@",error.userInfo] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"重新注册" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
        }
    }];
}


// 判断邮箱格式
-(BOOL)isValidateEmail:(NSString *)email

{
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
    
}





@end
